// pages/user/index.js
const http = require('../../utils/http.js')
const config = require('../../config.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrcode: "", //用户二维码
    qiNiuToken: '',
    //用户信息数据
    avatar: '', //头像
    name: '', //名字
    info: '', //公司
    phone: '', //电话
    articId: '', //文章ID
    upQrcode: '', //向服务器提交的二维码地址
    loading: false, //是否显示按钮点击状态
    disabled: false,  //是否禁用按钮点击
    userId: '', //用户ID
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.articId)
    this.setData({
      articId: options.articId
    })
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  /**
   * 获取用户信息
   */
  getUserInfo: function() {
    var that = this
    var data = {

    }
    http.httpReq(config.userInfo,'GET',data,function(res){
      console.log(res)
      var avatar = res.data.avatar
      var qrcode = app.handleImg(res.data.qrcode)
      that.setData({
        avatar: avatar,
        name: res.data.name,
        info: res.data.info,
        phone: res.data.phone,
        userId: res.data.id,
        qrcode: qrcode
      })
      console.log(that.data.userId)
    })
  },
  /**
   * 点击上传二维码
   */
  upImgTap: function() {
    var that = this
    that.getQiniuToken()
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var filePath = res.tempFilePaths[0];
        var date = Date.parse(new Date()) + that.data.userId + '.jpg';
        console.log(date)
        wx.uploadFile({
          url: 'https://upload.qiniu.com',
          filePath: filePath,
          name: 'file',
          formData: {
            'key': date,
            'token': that.data.qiNiuToken
          },
          success: function (res) {
            var data = JSON.parse(res.data)
            var upQrcode = data.key
            var qrcode = app.handleImg(data.key)
            that.setData({
              qrcode: qrcode,
              upQrcode
            })
          },
          fail(error) {
            console.log(error)
          }
        });
      }
    })
  },
  /**
   * 获取七牛token
   */
  getQiniuToken: function() {
    var that = this
    var data = {}
    http.httpReq(config.tokenUrl, 'POST', data, function(res){
      that.setData({
        qiNiuToken: res.data
      })
    })
  },
  /**
   * 点击输入名字
   */
  nameTap: function(e) {
    console.log(e.detail.value)
    this.setData({
      name: e.detail.value
    })
  },
  /**
   * 点击输入公司
   */
  infoTap: function (e) {
    console.log(e.detail.value)
    this.setData({
      info: e.detail.value
    })
  },
  /**
   * 点击输入电话
   */
  phoneTap: function (e) {
    console.log(e.detail.value)
    this.setData({
      phone: e.detail.value
    })
  },
  /**
   * 点击完成
   */
  doneTap: function() {
    var that = this
    console.log(that.data.articId)
    if (that.data.phone.length == 0 || that.data.phone == undefined) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }
    if (that.data.info.length == 0 || that.data.info == undefined) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }
    if (that.data.name.length == 0 || that.data.name == undefined) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }
    if (that.data.qrcode.length == 0) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }
    that.setData({
      loading: true,
      disabled: true
    })
    var data = {
      phone: that.data.phone,
      info: that.data.info,
      name: that.data.name,
      qrcode: that.data.upQrcode,
      articleId: that.data.articId
    }
    http.httpReq(config.midUserInfo,'POST',data,function(res){
      console.log(res)
      that.setData({
        loading: false,
        disabled: false
      })
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];   //当前页面
      var prevPage = pages[pages.length - 2];  //上一个页面
      //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
      prevPage.setData({
        flgName: "share",
        flgId: res.data.id,
        getArticData: config.formUserArticle,
        parentId: that.data.userId,
        shareNick: that.data.name
      })
      wx.navigateBack({
        url: '../index/index'
      })
    })
  }
})